package com.eblacorp.archive.view.bean.transactionfile;

import java.beans.IntrospectionException;
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;

import org.icefaces.ace.component.fileentry.FileEntry;
import org.icefaces.ace.component.fileentry.FileEntryEvent;
import org.icefaces.ace.component.fileentry.FileEntryResults;
import org.icefaces.impl.event.FacesMessagesPhaseListener;

import com.eblacorp.archive.domain.fn.TransactionFile;
import com.eblacorp.archive.exceptions.NotFnDocumentException;
import com.eblacorp.archive.exceptions.NullValueException;
import com.eblacorp.archive.service.TransactionFileService;
import com.eblacorp.archive.view.utils.FacesUtils;
import com.icesoft.faces.context.effects.JavascriptContext;

@ManagedBean
@SessionScoped
public class TransactionFileSearchBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8460971554261056449L;
	
	@ManagedProperty(value = "#{transactionFileService}")
	private TransactionFileService transactionFileService;
	
	private List<TransactionFile> searchResault;
	private TransactionFile searchCriteria;
	private TransactionFile transactionFile;
	private Date dateFrom;
	private Date dateTo;
	private File file;
	private boolean editMode;
	private String url;

	@PostConstruct
	public void init(){
		searchCriteria = new TransactionFile();
		transactionFile = new TransactionFile();
		dateFrom = new Date();
		dateTo = new Date();
	}
	
	public String getSearchResaultList() {
		try {
			searchResault = transactionFileService.search(searchCriteria, dateFrom, dateTo, null, null, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String addTransactionDoc(){
		try {
			if(!editMode){
				transactionFileService.addNewDocument(transactionFile, null);
			}else{
				transactionFileService.updateProperties(transactionFile);
				editMode = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public String delete(){
		
		try {
			int index = Integer.parseInt(FacesUtils.getParam("deletedIndex"));
			transactionFileService.delete(searchResault.get(index), false);
			searchResault.remove(index);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public String edit(int index){
		
		try {
			transactionFile = searchResault.get(index);
			editMode = true;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public String viewDoc(int index){
		url = transactionFileService.getViewerUrl(searchResault.get(index));
		return null;
	}
	
	public String reset(){
		transactionFile = new TransactionFile();
		editMode = false;
		return null;
	}
	
	
	
	public void fileUploadListener(FileEntryEvent e){
		FileEntry fe = (FileEntry)e.getComponent();
        FileEntryResults results = fe.getResults();
        for(FileEntryResults.FileInfo info : results.getFiles()){
        	if(info.isSaved()){
        		File file = info.getFile();
                if (file != null) {
                	this.file = file;
                }else{
                	
                }
        	}
        }
        FacesContext.getCurrentInstance().getMessages().remove();
        
        
	}
	
	
	
	
	
	
	
	
	
	
	public TransactionFileService getTransactionFileService() {
		return transactionFileService;
	}
	
	public void setTransactionFileService(
			TransactionFileService transactionFileService) {
		this.transactionFileService = transactionFileService;
	}
	
	public List<TransactionFile> getSearchResault() {
		return searchResault;
	}

	public void setSearchResault(List<TransactionFile> searchResault) {
		this.searchResault = searchResault;
	}

	public TransactionFile getSearchCriteria() {
		return searchCriteria;
	}

	public void setSearchCriteria(TransactionFile searchCriteria) {
		this.searchCriteria = searchCriteria;
	}

	public Date getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(Date dateFrom) {
		this.dateFrom = dateFrom;
	}

	public Date getDateTo() {
		return dateTo;
	}

	public void setDateTo(Date dateTo) {
		this.dateTo = dateTo;
	}

	public TransactionFile getTransactionFile() {
		return transactionFile;
	}

	public void setTransactionFile(TransactionFile transactionFile) {
		this.transactionFile = transactionFile;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	
}
